/*
 * Copyright (C) 2021 ShangHai Quicktron Intelligent Technology Co.,Ltd
 * All Rights Reserved.
 * 本软件为上海快仓智能科技开发研制。未经本公司正式书面同意，其他任何个人、团体
 * 不得使用、复制、修改或发布本软件.
 * 版权所有 翻版必究
 */

package com.kc.gb.device.service.support.dto;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@ToString
public class LightState {
    //历史代码 表示灯亮着并且常亮
    private volatile boolean isOn;
    private volatile boolean isPressed;
    private volatile boolean isLedOn;
    private volatile Short ledNum;
    //历史代码 表示灯亮着并且闪烁
    private volatile boolean isFlash;
    private volatile boolean stateChanged;
    private volatile int downUp;
    private volatile int weight;
    private volatile boolean isQuakeOn;
    @Getter
    @Setter
    private volatile boolean isLocked;

    /**
     * 昊方电子标签支持以下颜色
     * Off(0),
     * Red(32),
     * Green(64),
     * Blue(96),
     * Yellow(128),
     * Magenta(160),
     * Cyan(192),
     * White(224);
     */
    // @Getter
    // @Setter
    // private volatile LightColor lightColor = LightColor.values()[0];
    /**
     * 昊方用
     * 灯闪的频率
     */
    // @Getter
    // @Setter
    // private volatile LightOnOffRatio lightOnOffRatio = LightOnOffRatio.values()[0];
    /**
     * 昊方用
     * 灯闪的周期
     */
    // @Getter
    // @Setter
    // private volatile LightOnOffPeriod lightOnOffPeriod = LightOnOffPeriod.values()[0];

    public LightState() {
        isOn = false;
        isPressed = false;
    }

    public LightState(boolean isOn) {
        this.isOn = isOn;
    }

    public LightState(boolean isOn, boolean isPressed, boolean isLedOn, Short ledNum, boolean isQuakeOn) {
        this.isOn = isOn;
        this.isPressed = isPressed;
        this.isLedOn = isLedOn;
        this.ledNum = ledNum;
        this.isQuakeOn = isQuakeOn;
    }

    public LightState(boolean isOn, boolean isPressed, boolean isLedOn, Short ledNum, boolean isQuakeOn,
                      boolean isFlash) {
        this.isOn = isOn;
        this.isPressed = isPressed;
        this.isLedOn = isLedOn;
        this.ledNum = ledNum;
        this.isQuakeOn = isQuakeOn;
        this.isFlash = isFlash;
    }

    // public LightState(boolean isOn, boolean isPressed, boolean isLedOn, Short ledNum, boolean isQuakeOn,
    //                   boolean isFlash, boolean isLocked, LightColor lightColor) {
    //     new LightState(isOn, isPressed, isLedOn, ledNum, isQuakeOn, isFlash);
    //     this.isLocked = isLocked;
    //     this.lightColor = lightColor;
    // }

    public boolean isOn() {
        return isOn;
    }

    public void setOn(boolean isOn) {
        this.isOn = isOn;
    }

    public boolean isPressed() {
        return isPressed;
    }

    public void setPressed(boolean isPressed) {
        this.stateChanged = (this.isPressed != isPressed);
        this.isPressed = isPressed;
        downUp = downUp << 1;
        downUp = downUp | (isPressed ? 0x01 : 0x00);
    }

    public boolean isLedOn() {
        return isLedOn;
    }

    public void setLedOn(boolean isLedOn) {
        this.isLedOn = isLedOn;
    }

    public Short getLedNum() {
        return ledNum;
    }

    public void setLedNum(Short ledNum) {
        this.ledNum = ledNum;
    }

    public boolean isStateChanged() {
        return stateChanged;
    }

    public void setStateChanged(boolean stateChanged) {
        this.stateChanged = stateChanged;
    }

    public boolean isDownUp() {
        return (downUp & 0x03) == 0x02;
    }

    public boolean isFlash() {
        return isFlash;
    }

    public void setFlash(boolean isFlash) {
        this.isFlash = isFlash;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public boolean isQuakeOn() {
        return isQuakeOn;
    }

    public void setQuakeOn(boolean isQuakeOn) {
        this.isQuakeOn = isQuakeOn;
    }


    public void turnOn() {
        this.setFlash(false);
        this.setOn(true);
        this.setLocked(true);
    }

    public void turnOff() {
        this.setFlash(false);
        this.setOn(false);
        this.setLocked(true);
        turnOffLed();
    }

    public void flash() {
        this.setFlash(true);
        this.setOn(false);
        this.setLocked(true);
    }

    public void turnOnLed(short num) {
        this.setLedOn(true);
        this.setLedNum(num);
        this.setLocked(true);
    }

    public void turnOffLed() {
        this.setLedOn(false);
        this.setLedNum(null);
        this.setLocked(true);
    }

    public void waitForLightPress() {
        setLocked(false);
    }

    public void stopPressWait() {
        setLocked(true);
    }

}
